package parser;

import lexer.Token;

/**
 * The main parser call for the program. Compiles the Tokens into a LogoProgram
 * object Also performs the identifiers check.
 */
public class LogoParser {

	/**
	 * Parses the passed Token Array into a LogoProgram structure
	 * 
	 * @param Token Array
	 * @return A valid LogoProgram
	 * @throws LogoParseException if a compilation error occurs
	 */
	public static LogoProgram parseTokens(Token[] tokens) throws LogoParseException {
		LogoProgram program = null;
		// Scope stores all identifiers.
		LogoScope scope = new LogoScope();

		TokenIterator it = new TokenIterator(tokens);

		// First we parse the tokens into a LogoProgram, which contains LogoProcedures etc.
		// This process might hit problems such as invalid token order.
		program = new LogoProgram(it, scope);

		// Now we have the LogoScope initialised (by parsing the program to begin with)
		// We now have to check all identifiers are correct
		program.resolveIdentifiers(scope);

		return program;
	}
}
